

****DEFINE GROUPS**** (ENABLES IVOL ESTIMATES TO RUN MUCH MORE QUICKLY)
use PERMNO date RET DLRET DLSTCD using "$jfqa_rep/crsp_daily_all", clear

qui{
rename PERMNO permno
rename RET ret
rename DLSTCD dlstcd
rename DLRET dlret

drop if missing(ret)

*de-listed returns
replace ret=dlret if missing(ret)
replace ret=-0.3 if missing(ret) & dlstcd==500
replace ret=-0.3 if missing(ret) & dlstcd==520
replace ret=-0.3 if missing(ret) & dlstcd>550 & dlstcd<575
replace ret=-0.3 if missing(ret) & dlstcd==580
replace ret=-0.3 if missing(ret) & dlstcd==584
replace ret=-1 if missing(ret) & ~missing(dlstcd)
drop dlstcd dlret

gen year=year(date)
gen month=month(date)
gen day=day(date)

sort permno date
drop if permno==permno[_n-1] & date==date[_n-1]

drop date

merge m:1 year month day using "$jfqa_rep/FF3_Daily.dta"

gen excess_ret=ret-rf/100
replace mktrf=mktrf/100

drop _merge rf

preserve

tempfile copy

keep permno

drop if permno==permno[_n-1]

duplicates drop

egen permno_group=xtile(permno), nq(100)

save `copy'

restore 

merge m:1 permno using `copy'
drop _merge

}
save "$jfqa_rep/13_IVOL_Groups.dta", replace


qui{
forval i=1/100 {
use "$jfqa_rep/13_IVOL_Groups.dta", clear

keep if permno_group==`i'
drop permno_group ret
drop if missing(excess_ret)

gen monthyear=ym(year, month)

sort permno monthyear 
foreach j in mktrf smb hml {
replace `j'=`j'/100
}

statsby e(rmse), by(permno monthyear) saving("$jfqa_rep/13_iv_`i'", replace): reg excess_ret mktrf smb hml
}
} 

*****append volatility files
use "$jfqa_rep/13_iv_1", clear

forval i=2/100 {
append using "$jfqa_rep/13_iv_`i'"
}

/*
forval i==1/100 {
	rm "$jfqa_rep/13_iv_`i'.dta"
}
rm "$jfqa_rep/13_IVOL_Groups.dta"
*/

rename _stat_1 iv_ff3

sort permno monthyear

save "$jfqa_rep/13_IVOL.dta", replace
